﻿using System;
using System.Xml.Linq;

namespace Eswat
{
	/// <summary>
	/// 
	/// </summary>
	public class TestResultsXmlFormatter
	{
		private readonly string _path;

		private XDocument _doc;

		private XElement _fileTest;

		/// <summary>
		/// Initializes a new instance of the <see cref="TestResultsXmlFormatter"/> class.
		/// </summary>
		/// <param name="path">The path.</param>
		public TestResultsXmlFormatter(string path)
		{
			this._path = path;
		}

		/// <summary>
		/// Opens this instance.
		/// </summary>
		public void Open()
		{
			this._doc = new XDocument(
				new XDeclaration("1.0", "UTF-8", "true"),
				new XElement("eswat",
					new XAttribute("id", Guid.NewGuid()),
					new XAttribute("name", Environment.UserName + "@" + Environment.MachineName + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")),
					new XAttribute("runUser", Environment.MachineName + "\\" + Environment.UserName)

				//new XElement("ResultSummary",
				//    new XAttribute("outcome", "Completed"),
				//    new XElement("Counters",  // TODO: counters
				//        new XAttribute("total", "1"),
				//        new XAttribute("exeucted", "1"),
				//        new XAttribute("passed", "1"),
				//        new XAttribute("error", "0"),
				//        new XAttribute("failed", "0"),
				//        new XAttribute("timeout", "0"),
				//        new XAttribute("aborted", "0"),
				//        new XAttribute("inconclusive", "0"),
				//        new XAttribute("passedButRunAborted", "0"),
				//        new XAttribute("notRunnable", "0"),
				//        new XAttribute("notExecuted", "0"),
				//        new XAttribute("disconnected", "0"),
				//        new XAttribute("warning", "0"),
				//        new XAttribute("completed", "0"),
				//        new XAttribute("inProgress", "0"),
				//        new XAttribute("pending", "0")
				//    )
				//),
				//new XElement("TestDefinitions", // TODO
				//    new XElement("UnitTest",
				//        new XAttribute("name", "Instance_Creation"),
				//        new XAttribute("storage", @"c:\__code\rx\dev\tests\rx.qore.tests.unit\bin\debug\rx.qore.tests.unit.dll"),
				//        new XAttribute("id", Guid.NewGuid()),
				//        new XElement("Execution",
				//            new XAttribute("id", "d25fe829-3406-495f-a3d0-119612197bc1")
				//        ),
				//        new XElement("TestMethod",
				//            new XAttribute("codeBase", "C:/__code/Rx/Dev/tests/Rx.Qore.Tests.Unit/bin/Debug/Rx.Qore.Tests.Unit.DLL"),
				//            new XAttribute("adapterTypeName", "Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
				//            new XAttribute("className", "Rx.Qore.Tests.Unit.SurveyTester, Rx.Qore.Tests.Unit, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"),
				//            new XAttribute("name", "Instance_Creation")
				//        )
				//    )
				//),
				//new XElement("TestLists",
				//    new XElement("TestList",
				//        new XAttribute("name", "Results Not in a List"),
				//        new XAttribute("id", "8c84fa94-04c1-424b-9868-57a2d4851a1d")
				//    ),
				//    new XElement("TestList",
				//        new XAttribute("name", "All Loaded Results"),
				//        new XAttribute("id", "19431567-8539-422a-85d7-44ee4e166bda")
				//    )
				//),
				//new XElement("TestEntries",
				//    new XElement("TestEntry",
				//        new XAttribute("testId", "65a2ef5d-d85e-1ad6-fde0-01cdd7059f69"),
				//        new XAttribute("executionId", "d25fe829-3406-495f-a3d0-119612197bc1"),
				//        new XAttribute("testListId", "8c84fa94-04c1-424b-9868-57a2d4851a1d")
				//    )
				//),
				//new XElement("Results",
				//    new XElement("UnitTestResult",
				//        new XAttribute("executionId", "d25fe829-3406-495f-a3d0-119612197bc1"),
				//        new XAttribute("testId", "65a2ef5d-d85e-1ad6-fde0-01cdd7059f69"),
				//        new XAttribute("testName", "Instance_Creation"),
				//        new XAttribute("computerName", Environment.MachineName),
				//        new XAttribute("duration", "00:00:00.0258297"),
				//        new XAttribute("startTime", DateTime.Now.ToLocalTime()),
				//        new XAttribute("endTime", DateTime.Now.ToLocalTime()),
				//        new XAttribute("testType", "13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b"),
				//        new XAttribute("outcome", "Passed"),
				//        new XAttribute("testListId", "8c84fa94-04c1-424b-9868-57a2d4851a1d"),
				//        new XAttribute("relativeResultsDirectory", "d25fe829-3406-495f-a3d0-119612197bc1")
				//    )
				//)
				)
			);
		}

		/// <summary>
		/// Called when [test started].
		/// </summary>
		/// <param name="filePath">The file path.</param>
		public void OnTestStarted(string filePath)
		{
			this._fileTest = new XElement("TestResults",
				new XAttribute("startTime", DateTime.Now.ToLocalTime().ToString()));

			this._doc.Root.Add(this._fileTest);
		}

		/// <summary>
		/// Writes the specified new test result.
		/// </summary>
		/// <param name="newTestResult">The new test result.</param>
		public void Write(TestResult newTestResult)
		{
			this._fileTest.Add(new XElement("TestResult",
				new XAttribute("description", newTestResult.TestDescription),
				new XAttribute("outcome", newTestResult.Status.Name),
				new XText(newTestResult.ResultDescription)));
		}

		/// <summary>
		/// Called when [test completed].
		/// </summary>
		/// <param name="filePath">The file path.</param>
		public void OnTestCompleted(string filePath)
		{
			this._fileTest.SetAttributeValue("endTime", DateTime.Now.ToLocalTime().ToString());
			this._fileTest.SetAttributeValue("filePath", filePath);
		}

		/// <summary>
		/// Closes this instance.
		/// </summary>
		public void Close()
		{
			this._doc.Save(this._path);
		}
	}
}
